package com.itheima.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.POJO.Menu;
import com.itheima.POJO.Setmeal;
import com.itheima.dao.MenuMapper;
import com.itheima.entity.PageResult;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> findMenu() {
        List<Menu> list=menuMapper.findMenu();
        for (Menu menu : list) {
            List<Menu> children = menu.getChildren();
            for (int i = 0; i <  children.size(); i++) {
                Integer disable = children.get(i).getDisable();
                if (disable == 0){
                    children.remove(children.get(i));
                    i--;
                 //   i++;
                }

            }
        }
        return list;
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize) {
      /*
      <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="菜单名称" align="center"></el-table-column>
                <el-table-column prop="linkUrl" label="链接" align="center"></el-table-column>
                <el-table-column prop="keyword" label="权限点代码" align="center"></el-table-column>
                <el-table-column prop="path" label="排序号" align="center"></el-table-column>
                */
        PageHelper.startPage(currentPage,pageSize);
        Page<Menu> menuPage= menuMapper.findPage();
        List<Menu> result = menuPage.getResult();
        for (Menu menu : result) {
          List<String> keywordList=  menuMapper.findKeywordList(menu.getId());
          menu.setRoleKeywordList(keywordList);
        }
        return new PageResult(menuPage.getTotal(),menuPage.getResult());
    }

    @Override
    public void addmenu(Menu menu) {
        menuMapper.addmenu(menu);
    }

    @Override
    public Menu findMenuByid(Integer id) {
      Menu  menu=  menuMapper.findMenuByid(id);
        return menu;
    }

    @Override
    public void editMenu(Menu menu) {
        menuMapper.editMapper(menu);
    }

    @Override
    public void delete(Integer id) {
        menuMapper.delete(id);
    }


}
